'''
Created on 23/11/2010

@author: Nami
'''

import pyglet
from gameobjects.vector2 import Vector2
from game.effect.EffectLibrary import Effect, EFFECT_TRAIL

#-------------------------------------------------------------------------------

class Trail_Info(object):
    
    order = 0
    sprite = None
    location = Vector2(0,0)
    
    def __init__(self, order, sprite, location):
        self.order = order
        self.sprite = sprite
        self.location = location

class Effect_Trail(Effect):
    '''
    classdocs
    '''

    trail_images = []
    
    last_trail_order = 0
    
    trail_max = 0
    trail_time_step = 0
    
    draw_batch = None
    
    current_time_step = 0

    def update(self, elapsed):
        
        Effect.update(self, elapsed)
        
        if self.trail_max <= 0 or self.trail_time_step <= 0:
            return
        
        if not self.parent.sprite.image:
            return
        
        self.current_time_step += elapsed
        if self.current_time_step > self.trail_time_step:
            self.current_time_step -= self.trail_time_step
            self.last_trail_order += 1
            new_sprite = pyglet.sprite.Sprite(self.parent.sprite.image, batch = self.draw_batch)
            new_sprite.
            new_sprite.position = self.parent.sprite.position
            new_sprite.scale = self.parent.sprite.scale
            new_sprite.rotation = self.parent.sprite.rotation
            trail_element = Trail_Info(self.last_trail_order,new_sprite,Vector2(*self.parent.sprite.position))
            self.trail_images.insert(0,trail_element)
            self.trail_images = self.trail_images[:self.trail_max]

    def render(self):
        if len(self.trail_images) == 0:
            return
        
        render_list = list(self.trail_images)
        render_list.reverse()
        trans_step = 185/len(self.trail_images)
        
        current_element = 0
        
        self.draw_batch.draw()
        
        #for trail_element in render_list:
            #trail_element.opacity = 0.5
        #    trail_element.sprite.draw()

    def __init__(self,trail_max = 0, trail_time_step = 0, duration = -1):
        '''
        Constructor
        '''
        Effect.__init__(self, EFFECT_TRAIL, duration)
        
        self.trail_max = trail_max
        self.trail_time_step = trail_time_step
        self.duration = duration
        self.draw_batch =  pyglet.graphics.Batch()
#-------------------------------------------------------------------------------